package com.proton.carepatchtemp.login.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.dao.RegisterInfo;
import com.proton.carepatchtemp.utils.FormUtils;
import com.proton.carepatchtemp.utils.NumUtils;
import com.proton.carepatchtemp.utils.VaildUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.SignCenter;


public class FindPwdActivity extends BaseActivity implements View.OnClickListener {
    RegisterInfo info = new RegisterInfo();
    private ImageButton mBtn_back_findpwd;
    private EditText mEt_phone_number;
    private EditText mEt_new_pwd;
    private EditText mEt_verification_code;
    private TextView mTv_send_verification;
    private FrameLayout mFrameLayout_timer;
    private ImageView mIv_timer;
    private TextView mTv_timer;
    private TextView mTv_complete;
    private boolean complete = false;

    @Override
    public void dealEvent(MainEvent event) {
        if (null == event || isFinishing()) {
            return;
        }
        int id = event.getId();
        switch (id) {
            case Codes.NET_SIGN_FPWD_GET_CAP:
                if (event.isSuccess() || event.isInterval()) {
                    if (event.isInterval()) {
                        Toast.makeText(this, R.string.get_too_much_captime, Toast.LENGTH_LONG).show();
                    }
                    int capTime = NumUtils.getIntNum(event.getMsg());
                    capTime = capTime == 0 ? -1 : capTime;
                    info.setCapTime(capTime);
                    sendMsg();
                    //dealGetRightCaptcha(capTime);
                } else {
                    Toast.makeText(this, event.getMsg(), Toast.LENGTH_LONG).show();
                }
                break;
            case Codes.NET_SIGN_FPWD_VALI_CAP:
                if (event.isSuccess()) {
                    String msg = event.getMsg();
                    info.setCode(msg);
                    dealValiFPwdSuccess();
                } else {
                    Toast.makeText(this, event.getMsg(), Toast.LENGTH_LONG).show();
                }
                break;
            case Codes.NET_SIGN_FPWD_CHNAGE:
                if (event.isSuccess()) {
                    Intent completeIntent = new Intent(FindPwdActivity.this, PwdSucceedActivity.class);
                    startActivity(completeIntent);
                    finish();
                } else {
                    Toast.makeText(this, event.getMsg(), Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.find_pwd_activity);
        //  初始化View
        bindViews();
        //  设置控件监听
        setListener();
    }

    /**
     * view初始化
     */
    private void bindViews() {
        mBtn_back_findpwd = findViewById(R.id.btn_back_findpwd);
        mEt_phone_number = findViewById(R.id.et_phone_number);
        mEt_new_pwd = findViewById(R.id.et_new_pwd);
        mEt_verification_code = findViewById(R.id.et_verification_code);
        mTv_send_verification = findViewById(R.id.tv_send_verification);
        mFrameLayout_timer = findViewById(R.id.count_down_timer);
        mIv_timer = findViewById(R.id.iv_timer);
        mTv_timer = findViewById(R.id.tv_timer);
        mTv_complete = findViewById(R.id.tv_complete);
    }

    /**
     * 初始化设置各控件监听器
     */


    @Override
    public void setListener() {
        //  返回按钮
        mBtn_back_findpwd.setOnClickListener(this);

        //  发送验证码
        mTv_send_verification.setOnClickListener(this);

        //  完成按钮
        mTv_complete.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            //  返回按钮
            case R.id.btn_back_findpwd:
                finish();
                break;

            //  发送验证码
            case R.id.tv_send_verification:
                if (canReSetPwd()) {
                    SignCenter.getFPwdCap(info.getMobile(), 5);
                }
                break;

            case R.id.tv_complete:
                dealGetRightCaptcha();
                break;
            default:
                break;
        }
    }


    /**
     * 发送获取验证码请求
     */
    private void sendMsg() {
        Toast.makeText(this, R.string.post_verification_success, Toast.LENGTH_SHORT).show();
        mTv_send_verification.setVisibility(View.INVISIBLE);
        mFrameLayout_timer.setVisibility(View.VISIBLE);
        final RotateAnimation rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        mIv_timer.setAnimation(rotateAnimation);
        new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTv_timer.setTextColor(Color.parseColor("#7F7F7F"));
                mTv_timer.setText("" + (int) (millisUntilFinished / 1000));
                mIv_timer.startAnimation(rotateAnimation);
            }

            @Override
            public void onFinish() {
                //  倒计时完成后恢复
                mIv_timer.clearAnimation();
                mTv_send_verification.setVisibility(View.VISIBLE);
                mFrameLayout_timer.setVisibility(View.INVISIBLE);
            }
        }.start();
    }

    public boolean canReSetPwd() {

        String phone = FormUtils.getString(mEt_phone_number);
        String pwd = FormUtils.getString(mEt_new_pwd);
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, R.string.no_value_phone_number, Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, R.string.no_value_pwd, Toast.LENGTH_SHORT).show();
            return false;
        }
        if (VaildUtils.valiMobile(phone)) {
            info.setMobile(phone);
        } else {
            Toast.makeText(this, R.string.invalid_phone_number, Toast.LENGTH_SHORT).show();
            return false;
        }
        if (VaildUtils.valiPwd(pwd)) {
            info.setPwd(pwd);
        } else {
            Toast.makeText(this, R.string.invalid_pwd, Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }


    @Override
    public void onEventMainThread(MainEvent event) {
        dealOnEvent(event);
    }

    private void dealValiFPwdSuccess() {
        boolean result = SignCenter.changePwd(info);
    }

    private void dealGetRightCaptcha() {
        String captcha = FormUtils.getString(mEt_verification_code);
        if (TextUtils.isEmpty(captcha)) {
            Toast.makeText(this, R.string.no_value_captcha, Toast.LENGTH_SHORT).show();
            return;
        }
        SignCenter.valiFPwdCap(info.getMobile(), captcha);
    }
}
